import tkinter as tk  # 使用Tkinter前需要先导入
import os
import subprocess
from tkinter import messagebox
path = os.path.abspath('.')
# 第1步，实例化object，建立窗口window
window = tk.Tk()

# 第2步，给窗口的可视化起名字
window.title('应急车辆车速引导系统')

# 第3步，设定窗口的大小(长 * 宽)
window.geometry('800x700')  # 这里的乘是小x

# 第4步，加载 welcome image
canvas = tk.Canvas(window, width=800, height=60, bg='orange')
image_file = tk.PhotoImage(
    file=path + '\GUI\emey.gif')
image = canvas.create_image(400, 0, anchor='n', image=image_file)
canvas.pack(side='top')
tk.Label(window, text='Welcome Here', font=('Arial', 16), anchor='nw').pack()

# 第5步，用户信息
tk.Label(window, text='应急车辆:', font=('Arial', 18)).place(x=10, y=100)
tk.Label(window, text='排队车列:', font=('Arial', 18)).place(x=10, y=200)

# 第6步，emergency
# distance
tk.Label(window, text='L0:（距离）', font=('Arial', 18)).place(x=120, y=130)
emergency_distance = tk.StringVar()
emergency_distance.set(100)
entry_emergency_distance = tk.Entry(
    window, textvariable=emergency_distance, font=('Arial', 14))
entry_emergency_distance.place(x=260, y=130)
# speed
tk.Label(window, text='V0:（速度）', font=('Arial', 18)).place(x=120, y=170)
emergency_speed = tk.StringVar()
emergency_speed.set(8)
entry_emergency_speed = tk.Entry(
    window, textvariable=emergency_speed, font=('Arial', 14))
entry_emergency_speed.place(x=260, y=170)

# 第7步，team
# distance
tk.Label(window, text='L1:（距离）', font=('Arial', 18)).place(x=120, y=230)
team_distance = tk.StringVar()
team_distance.set(20)
entry_team_distance = tk.Entry(
    window, textvariable=team_distance, font=('Arial', 14))
entry_team_distance.place(x=260, y=230)
# speed
tk.Label(window, text='V1:（速度）', font=('Arial', 18)).place(x=120, y=270)
team_speed = tk.StringVar()
team_speed.set(1)
entry_team_speed = tk.Entry(
    window, textvariable=team_speed, font=('Arial', 14))
entry_team_speed.place(x=260, y=270)

# 第8步，信号灯状态
tk.Label(window, text='信号灯状态:（0红1绿）', font=('Arial', 18)).place(x=10, y=310)
tk.Label(window, text='灯:（状态）', font=('Arial', 18)).place(x=120, y=350)
var_status = tk.StringVar()
var_status.set(0)
light_status = tk.Entry(window, textvariable=var_status, font=('Arial', 14))
light_status.place(x=260, y=350)

# 第9步, 输出控制模块
tk.Label(window, text='车速引导结果：', font=('Arial', 18)).place(x=10, y=390)
text = tk.Text(window, height=3)
text.place(x=120, y=440)

# 第10步, variables
"""
@entry_emergency_distance --> emergency_distance
@entry_emergency_speed --> emergency_speed
@entry_team_distance --> team_distance
@entry_team_speed --> team_speed
@light_status --> var_status
@text 输出控制模块
"""


# 第11步，login and sign up 按钮
def Quit():
    quit()


# 读取结果文件
def readfile():
    file = open("app.log", "r", encoding='UTF-8')
    string = file.read()
    # print(str)
    file.close()
    text.insert("end", string)


def getVariables():
    # L0 = 100
    # V0 = 8
    # L1 = 20
    # V1 = 0
    # Green = 0
    L0 = emergency_distance.get()
    emergency_distance.set(100)
    V0 = emergency_speed.get()
    emergency_speed.set(8)
    L1 = team_distance.get()
    team_distance.set(10)
    V1 = team_speed.get()
    team_speed.set(0)
    Green = var_status.get()
    var_status.set(0)
    # print(L0, V0, L1, V1, Green)
    text.delete('1.0', 'end')
    cmd = f".\subtree.exe -l {L0} -m {L1} -v {V0} -w {V1} -g {Green}"
    # r_v = os.system("./subtree.exe")
    print(cmd)
    ret = subprocess.call(cmd, shell=True)
    readfile()


def process():
    txt = text.get('0.0', 'end')
    print(txt)
    messagebox.showinfo(title='采取策略', message=txt)


btn_login = tk.Button(window, text='确定', command=getVariables)
btn_login.place(x=267, y=650)
btn_sign_up = tk.Button(window, text='退出', command=Quit)
btn_sign_up.place(x=500, y=650)

btw = tk.Button(window, text='采取策略', command=process)
btw.place(x=200, y=390)

# 第10步，主窗口循环显示
window.mainloop()
